package com.mapresso.gps;
import java.awt.Color;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import com.vividsolutions.jts.geom.Coordinate;


public class GpsUtils {
	
	public static Coordinate[] path2coord(GpsTrack track) {
		Coordinate[] coord = new Coordinate[track.nPoints()]; 
		for (int i = 0; i < coord.length; i++) {
			coord[i] = new Coordinate(track.getPoint(i).getLongitude(), 
					track.getPoint(i).getLatitude(), track.getPoint(i).getElevation());
		}
		return coord;
	}

	public static GpsTrack coord2path(Coordinate[] coord) {
		Vector path = new Vector(coord.length);
		for (int i = 0; i < coord.length; i++) {
			path.add(new Location(coord[i].y, coord[i].x, coord[i].z));			
		}
		return new GpsTrack(path);
	}
	
	public static CoordinateTime[] path2coordTime(GpsTrack track) {
		CoordinateTime[] ct = new CoordinateTime[track.nPoints()]; 
		for (int i = 0; i < ct.length; i++) {
			ct[i] = new CoordinateTime(track.getPoint(i).getLongitude(), 
					track.getPoint(i).getLatitude(), track.getPoint(i).getElevation(),
					track.getPoint(i).getTimeAsStringGPX()); // getTimeString());  
		}
		return ct;
	}

	public static GpsTrack coordTime2path(CoordinateTime[] coord) {
			Vector path = new Vector(coord.length);
			for (int i = 0; i < coord.length; i++) {
				path.add(new Location(coord[i].y, coord[i].x, coord[i].z, coord[i].getDateTimeString()));			
			}
			return new GpsTrack(path);
		}

	public static GpsTrack coordTime2path(Coordinate[] coord,
			CoordinateTime[] coord_basis) {
		Vector path = new Vector(coord.length);
		int j = -1;
		for (int i = 0; i < coord.length; i++) {
			do {
				j++;
			} while (!coord[i].equals3D(coord_basis[j]));
			path.add(new Location(coord[i].y, coord[i].x, coord[i].z, coord_basis[j].getDateTimeString()));			
		}
		return new GpsTrack(path);
	}
	
	public static double angle2meter() {
		return 1000.0 * 40000.0 / 360.0;
	}

	public static double meter2angle() {
		return 1.0 / (1000.0 * 40000.0 / 360.0);
	}

	static String format(double x, int zahlStellen) {
		final NumberFormat defForm = NumberFormat.getInstance();
		if (Double.isNaN(x)) {
			return "NaN";
		}

		defForm.setMaximumFractionDigits(zahlStellen);
		defForm.setMinimumFractionDigits(zahlStellen);
		return defForm.format(x);
	}

    static String formatDate(Date d) {
    	SimpleDateFormat sd = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    	return sd.format(d);
    }

    static String formatDateYMD(Date d) {
    	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	return sd.format(d);
    }
    
    static String formatDateGPX(Date d) {
    	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
    	return sd.format(d);
    }
	public static double toRadians(double d) {
		return Math.PI * d / 180.0;
	}

	public static double[] CH1903ToWGS84(double y, double x, double altitude) {
		double[] result = new double[3];
		double y2 = (y - 200000) / 1000000;
		double x2 = (x - 600000) / 1000000;

		result[0] = toRadians((16.9023892 + 3.238272 * y2 - 0.270978 * x2 * x2
				- 0.002528 * y2 * y2 - 0.0447 * x2 * x2 * y2 - 0.014 * y2 * y2
				* y2) * 100 / 36);

		result[1] = toRadians((2.6779094 + 4.728982 * x2 + 0.791484 * x2 * y2
				+ 0.1306 * x2 * y2 * y2 - 0.0436 * x2 * x2 * x2) * 100 / 36);

		result[2] = altitude + 49.55 - 12.6 * x2 - 22.64 * y2;
		return result;
	}

	static public double[] WGS84toCH1903(double lat, double lon) {
		double posx, posy;

		double phi = lat * 3600;
		double lambda = lon * 3600;

		double phip = (phi - 169028.66) / 10000;
		double lambdap = (lambda - 26782.5) / 10000;

		posy = ((600072.37) + (211455.93) * lambdap - (10938.51) * lambdap
				* phip - (0.36) * lambdap * (phip * phip) - (44.54f) * (lambdap
				* lambdap * lambdap));
		posx = ((200147.07) + (308807.95) * phip + (3745.25)
				* (lambdap * lambdap) + (76.63) * (phip * phip) - (194.56)
				* (lambdap * lambdap) * phip + (119.79) * (phip * phip * phip));

		return new double[] {posx, posy};
	}

	public static double WGS84toCH1903(float lat1, float lon1) {
		double lat1f = (double) lat1;
		double lon1f = (double) lon1;
		return WGS84toCH1903(lat1, lon1);
	}

	public static void printAction(String string) {
		System.out.println("/// " + string);
	}
	
	static void printSeparatorline() {
		System.out.println("-------------------------------------------------");
	}



}
